	//
//  YJOrderDetailCell.swift
//  yuejian
//
//  Created by yanziye on 2017/10/26.
//  Copyright © 2017年 gyy. All rights reserved.
//

    import UIKit
    class OrderDetailCell: UITableViewCell{
        
        var train_id: NSString!
        var train: YJTrainingModel! {
            didSet {
                train_id = train.train_id
            }
        }
        var trainImageView: UIImageView!
        //定义训练名字title
        var trainNameTitle: UILabel!
        //定义训练名字
        var trainName: UILabel!
        //定义训练title
        var priceTitle: UILabel!
        //定义训练
        var price: UILabel!
        //定义推荐指数title
        var recommandTitle: UILabel!
        //定义推荐指数
        var recommand: UILabel!
        
        //定义训练编号title
        var shop_idTitle: UILabel!
        //定义训练编号
        var shop_id: UILabel!
        //购买数量
        var numTitle: UILabel!
        var num: UITextField!
        //总价
        var totalPriceTitle: UILabel!
        var totalPrice: UILabel!
        //当前时间
        var currentTime:String!
        //建议配送时间
        var suggestTime:String!
        //下单时间
        var orderTimeLabel: UILabel!
        //配送时间
        var deliverTimeLabel: UILabel!
        
        //确认btn
        var commitBtn: UIButton!
        //取消btn
        var cancelBtn: UIButton!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            trainImageView = UIImageView()
            self.contentView.addSubview(trainImageView)
            
            trainNameTitle = UILabel()
            trainNameTitle.text = "训练名称"
            trainNameTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(trainNameTitle)
            
            trainName = UILabel()
            self.contentView.addSubview(trainName)
            
            priceTitle = UILabel()
            priceTitle.text = "培训价格"
            priceTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(priceTitle)
            
            price = UILabel()
            self.contentView.addSubview(price)
            
            recommandTitle = UILabel()
            recommandTitle.text = "推荐指数"
            recommandTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(recommandTitle)
            
            recommand = UILabel()
            self.contentView.addSubview(recommand)
            
            shop_idTitle = UILabel()
            shop_idTitle.text = "培训编号"
            shop_idTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(shop_idTitle)
            
            shop_id = UILabel()
            self.contentView.addSubview(shop_id)
            
            numTitle = UILabel()
            numTitle.text = "培训人数"
            numTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(numTitle)
            
            num = UITextField()
            
            num.backgroundColor = UIColor.cyan
            num.font = UIFont.systemFont(ofSize: 16)
            num.text = "1"
            num.keyboardType = .decimalPad
            
            NotificationCenter.default.addObserver(self, selector: #selector(textfieldDidChange), name: .UITextFieldTextDidChange, object: num)
            
            self.contentView.addSubview(num)
            
            totalPriceTitle = UILabel()
            totalPriceTitle.text = "总价"
            totalPriceTitle.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(totalPriceTitle)
            
            totalPrice = UILabel()
            totalPrice.font = UIFont.systemFont(ofSize: 16)
            totalPrice.text = "23"
            self.contentView.addSubview(totalPrice)
            
            orderTimeLabel = UILabel()
            orderTimeLabel.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(orderTimeLabel)
            
            deliverTimeLabel = UILabel()
            deliverTimeLabel.font = UIFont.systemFont(ofSize: 16)
            self.contentView.addSubview(deliverTimeLabel)
            
            let nowDate = NSDate()
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm"
            currentTime = formatter.string(from: nowDate as Date)
            var timeInterval:TimeInterval = nowDate.timeIntervalSince1970
            let timeStamp = Int(timeInterval) + 1800
            timeInterval = TimeInterval(timeStamp)
            let date = NSDate(timeIntervalSince1970: timeInterval)
            suggestTime = formatter.string(from: date as Date)
            orderTimeLabel.text = "下单时间:\(currentTime!)"
            deliverTimeLabel.text = "审核时间:\(currentTime!)-\(suggestTime!)"
            
            let lastLine = CAShapeLayer()
            lastLine.path = UIBezierPath(rect: CGRect(x: 0, y: 230, width: ScreenWidth, height: 2)).cgPath
            lastLine.fillColor = UIColor.gray.cgColor
            self.contentView.layer.addSublayer(lastLine)
            
            commitBtn = UIButton()
            commitBtn.backgroundColor = UIColor.cyan
            commitBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
            commitBtn.setTitle("确定", for: UIControlState.normal)
            commitBtn.addTarget(self, action:#selector(commit) , for: UIControlEvents.touchUpInside)
            self.contentView.addSubview(commitBtn)
            
            cancelBtn = UIButton()
            cancelBtn.backgroundColor = UIColor.cyan
            cancelBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
            cancelBtn.setTitle("取消", for: UIControlState.normal)
            cancelBtn.addTarget(self, action:#selector(cancel) , for: UIControlEvents.touchUpInside)
            self.contentView.addSubview(cancelBtn)
        }
        func textfieldDidChange(){
            if num.text?.characters.count != 0 {
                let total = Float(num.text!)!*Float(price.text!)!
                totalPrice.text = "\(total)"
            }else{
                totalPrice.text = "0"
            }
        }
        
        func commit() {
            
            let parameters:NSDictionary = ["userid":train.userid,"train_id":train_id,"num":Int(num.text!)!,"sum":Double(totalPrice.text!)!,"suggesttime":"\(currentTime)-\(suggestTime)"]
            
            YJNetworkMngTool.sharedNetWorkMngTool().YJNetwork_insertOrderWithParameters(parameters, view: UIView(), block: { (flag) -> Void in
                if flag == "1" {
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Commit"), object: nil)
                }
            })
        }
        
        func cancel() {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Commit"), object: nil)
        }
        //2.设置控件位置
        override func layoutSubviews() {
            super.layoutSubviews()
            trainImageView.frame = CGRect(x: 0, y: 0, width: CGFloat(WHRatio * CELLHEIGHT), height: CELLHEIGHT);
            
            trainNameTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 0, width: 80, height: 20);
            
            trainName.frame = CGRect( x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 0,width: ScreenWidth-(WHRatio*CELLHEIGHT + MARGIN + 80), height: 20);
            
            priceTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 20, width: 80, height: 20);
            
            price.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 20,width: ScreenWidth-(WHRatio * CELLHEIGHT + MARGIN + 80), height: 20);
            
            recommandTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 40, width: 80, height: 20);
            
            recommand.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 40, width: ScreenWidth-(WHRatio * CELLHEIGHT + MARGIN + 80), height: 20);
            
            shop_idTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 60, width: 80, height: 20);
            
            shop_id.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 60, width: ScreenWidth-(WHRatio * CELLHEIGHT + MARGIN + 80), height: 20);
            
            numTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 80, width: 80, height: 20);
            num.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 80, width: 120, height: 20);
            
            totalPriceTitle.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 100, width: 80, height: 20);
            totalPrice.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN + 80, y: 100, width: 120, height: 20);
            
            orderTimeLabel.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 120, width: 200, height: 20);
            
            deliverTimeLabel.frame = CGRect(x: WHRatio * CELLHEIGHT + MARGIN, y: 140, width: 200, height: 20);
            
            commitBtn.frame = CGRect(x: MARGIN + 50, y: 250, width: 90, height: 30);
            
            cancelBtn.frame = CGRect(x:  MARGIN + 200, y: 250, width: 90, height: 30);
            
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
        }
        
    }

